package com.etl.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eksframework.commons.core.base.EksCopier;
import com.eksframework.commons.json.EksJson;
import com.etl.sys.entity.SysTenant;
import com.etl.sys.service.ISysTenantService;
import com.etl.sys.vo.TenantParam;
import com.etl.sys.vo.TenantVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 租户表 前端控制器
 * </p>
 *
 * @author chenfenghai
 * @since 2020-11-03
 */
@Slf4j
@RestController
@RequestMapping("/tenant")
public class SysTenantController {

    @Autowired
    ISysTenantService sysTenantService;

    @PostMapping("/page")
    public IPage<SysTenant> page(@RequestBody TenantParam params) {
        log.debug("getPage: {}", EksJson.toJson(params));
        QueryWrapper<SysTenant> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNoneBlank(params.getTenantCode())) {
            queryWrapper.like("TENANT_CODE", params.getTenantCode());
        }
        if (StringUtils.isNoneBlank(params.getTenantName())) {
            queryWrapper.like("TENANT_NAME", params.getTenantName());
        }
        IPage<SysTenant> page = new Page<>(params.getPageNumber(), params.getPageSize(), params.getTotal());
        return sysTenantService.page(page, queryWrapper);
    }

    @GetMapping("/list")
    public List<SysTenant> list(@RequestParam(name = "tenantCode", required = false) String tenantCode,
                                @RequestParam(name = "tenantName", required = false) String tenantName) {
        QueryWrapper<SysTenant> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNoneBlank(tenantCode)) {
            queryWrapper.like("TENANT_CODE", tenantCode);
        }
        if (StringUtils.isNoneBlank(tenantName)) {
            queryWrapper.like("TENANT_NAME", tenantName);
        }
        return sysTenantService.list(queryWrapper);
    }

    @GetMapping("/one")
    public SysTenant one(@RequestParam(name = "tenantId") Long tenantId) {
        return sysTenantService.getById(tenantId);
    }

    @PostMapping("/save")
    public void save(@Validated @RequestBody TenantVO tenantVO) {
        if (tenantVO.getTenantId() <= 0) {
            tenantVO.setTenantId(null);
            Integer count = sysTenantService.lambdaQuery()
                    .eq(SysTenant::getTenantCode, tenantVO.getTenantCode()).count();
            if (count > 0) {
                throw new IllegalArgumentException("院线编码不能重复，请重新填写");
            }
        }
        SysTenant sysTenant = EksCopier.copy(tenantVO, SysTenant.class);
        sysTenantService.saveOrUpdate(sysTenant);
    }

    @DeleteMapping("/del")
    public void del(@RequestParam(name = "tenantId") Long tenantId) {
        sysTenantService.lambdaUpdate()
                .set(SysTenant::getDeleteFlag, tenantId)
                .eq(SysTenant::getTenantId, tenantId)
                .update();
    }
}
